<?php
// Start the session
@session_start();

$id = session_id();
if ($id == "") {
	exit;
}

// Create folder for temporary data
$tmpDataFolder = "../tmp_data/" . $id;
if (!file_exists($tmpDataFolder)) {
	mkdir($tmpDataFolder);
}

// Create photos.xml file in tmp_data folder
$xml = new DOMDocument();
$photo = $xml->createElement("photo");

$photoId = $xml->createElement("id");
$photoIdText = $xml->createTextNode("1");
$photoId->appendChild($photoIdText);
$photo->appendChild($photoId);

$path = $xml->createElement("path");
$pathText = $xml->createTextNode("../upload/" . $id . "/face.jpg");
$path->appendChild($pathText);
$photo->appendChild($path);
$xml->appendChild($photo);

$xml->save($tmpDataFolder . "/photos.xml");

// Execute detect face
$output = exec(realpath(dirname(__FILE__) . "/../../detectFace/detectFace"). " " . realpath(dirname(__FILE__) . "/../tmp_data/" . $id), $output);

$array = explode("\n", $output);
$facePoints = array();
foreach($array as $element) {
	preg_match_all("/\d+/", $element, $matches);
	$result = array();
	array_push($result, $matches[0][0]); array_push($result, $matches[0][1]); array_push($result, $matches[0][2]); array_push($result, $matches[0][3]);
	$facePoints[] = $result;
}
echo json_encode($facePoints);

?>